package com.dhcc.sdc.base.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.entity.SdcBaseAssessmentEntity;
import com.dhcc.sdc.base.service.ISdcBaseAssessmenjudgementService;
import com.dhcc.sdc.base.service.ISdcBaseAssessmentNodeService;
import com.dhcc.sdc.base.service.ISdcBaseAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 
 * 
 * @ClassName: SdcBaseAssessmentController
 * @Description: TODO
 * @author jumper
 * @date 2021-09-14 16:35:21
 */
@Controller
@RequestMapping("${sys.backendPath}/sdc/evaldic")
public class SdcBaseAssessmentController extends BaseController {

	private String PREFIX = "/backend/sdc/evaldic/";

	@Autowired
	private ISdcBaseAssessmentService sdcBaseAssessmentService;
    @Autowired
    private ISdcBaseAssessmenjudgementService sdcBaseAssessmenjudgementService;
    @Autowired
    private ISdcBaseAssessmentNodeService sdcBaseAssessmentNodeService;
    /**
     * 跳转到评估字典列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "evaldic.html";
    }


    /**
     * 跳转到添加评估字典
     */
    @RequestMapping("/to_add")
    public String evaldicAdd() {
        return PREFIX + "evaldic_add.html";
    }
	
	/**
     * 跳转到修改
     */
    @RequestMapping("/to_update/{evalDicId}")
    public String toUpdate(@PathVariable Long evalDicId, Model model) {
        SdcBaseAssessmentEntity sdcBaseAssessment = this.sdcBaseAssessmentService.selectById(evalDicId);
        List<Map<String, String>> dicLevelInfo = this.sdcBaseAssessmenjudgementService.selectByAssessmentId(evalDicId);
        List<Map<String, String>> evalNodeInfo = this.sdcBaseAssessmentNodeService.selectByAssessmentId(evalDicId);
        model.addAttribute("evalDic",sdcBaseAssessment);
        model.addAttribute("dicLevelInfo", JSON.toJSONString(dicLevelInfo));
        model.addAttribute("evalNodeInfo", JSON.toJSONString(evalNodeInfo));
        return PREFIX + "evaldic_edit.html";
    }

    /**
     * 获取列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdcBaseAssessmentEntity> page = new PageFactory<SdcBaseAssessmentEntity>().defaultPage();
        List<SdcBaseAssessmentEntity> result = this.sdcBaseAssessmentService.page(page,params);
        page.setRecords((List<SdcBaseAssessmentEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 获取评估表的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        return sdcBaseAssessmentService.tree();
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增")
    public Object add(SdcBaseAssessmentEntity sdcBaseAssessment, String levelInfos, String evalNodes) {
        if (CommonUtil.isOneEmpty(sdcBaseAssessment, levelInfos)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        this.sdcBaseAssessmentService.addDict(sdcBaseAssessment, levelInfos,evalNodes);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除")
    public Object delete(@RequestParam Long sdcBaseAssessmentId) {
	
        this.sdcBaseAssessmentService.delteDict(sdcBaseAssessmentId);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改")
    public Object update(SdcBaseAssessmentEntity sdcBaseAssessment, String levelInfos, String evalNodes) {
        if (CommonUtil.isOneEmpty(sdcBaseAssessment, levelInfos)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        this.sdcBaseAssessmentService.editDict(sdcBaseAssessment, levelInfos, evalNodes);
        return R.ok();
    }
	
}
